// 窗口高度
var y_h = 0,
	y_w = 0;

// 监控窗口变化
//
window.onresize = function() {
	MonitorWin();
};

function MonitorWin() {
	y_h = document.documentElement.clientHeight;
	y_w = document.documentElement.clientWidth;

	console.log("查看", y_w, y_h);
}

// 截取URL后面的参数
var Tools = {
	getUrlData: function GetRequest() {
		var url = location.search; //获取url中"?"符后的字串
		var theRequest = new Object();
		if(url.indexOf("?") != -1) {
			var str = url.substr(1);
			strs = str.split("&");
			for(var i = 0; i < strs.length; i++) {
				theRequest[strs[i].split("=")[0]] = decodeURIComponent(strs[i].split("=")[1]);
			}
		}
		return theRequest;
	},

};

// 正则
var Pattern = {
	posPattern: /^\d+$/, //正整数正则
	negPattern: /^-\d+$/, //负整数正则
	intPattern: /^-?\d+$/, //整数正则
	//正数正则
	posPattern: /^\d*\.?\d+$/,
	//负数正则
	negPattern: /^-\d*\.?\d+$/,
	//数字正则
	numPattern: /^-?\d*\.?\d+$/,
	//Email正则
	ePattern: /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/,
	//手机号正则
	mPattern: /^1[34578]\d{9}$/, //http://caibaojian.com/regexp-example.html
	//身份证号（18位）正则
	cP: /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/,
	//URL正则
	urlP: /^((https?|ftp|file):\/\/)?([\da-z\.-]+)\.([a-z\.]{2,6})([\/\w \.-]*)*\/?$/,
	//ipv4地址正则
	ipP: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
	//RGB Hex颜色正则
	cPattern: /^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/,
	uPattern: /^[a-zA-Z0-9_-]{4,16}$/, //用户名正则，4到16位（字母，数字，下划线，减号）
	pPattern: /^.*(?=.{6,})(?=.*\d)(?=.*[A-Z])(?=.*[a-z])(?=.*[!@#$%^&*? ]).*$/, // 密码强度正则，最少6位，包括至少1个大写字母，1个小写字母，1个数字，1个特殊字符
	//日期正则，简单判定,未做月份及日期的判定
	dP1: /^\d{4}(\-)\d{1,2}\1\d{1,2}$/,
	//日期正则，复杂判定
	dP2: /^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$/,
	//车牌号正则
	cPattern: /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/,
	//包含中文正则
	cnPattern: /[\u4E00-\u9FA5]/,
	//QQ号正则，5至11位
	qqPattern: /^[1-9][0-9]{4,10}$/,
	//微信号正则，6至20位，以字母开头，字母，数字，减号，下划线
	wxPattern: /^[a-zA-Z]([-_a-zA-Z0-9]{5,19})+$/,
}

// 查詢某個元素
Array.prototype.queryOf = function(val) {
	for(var i = 0; i < this.length; i++) {
		if(this[i] == val) return i;
	}
	return -1;
};

/* 刪除數組某個元素
 * - val  删除的值 
 */
Array.prototype.remove = function(val) {
	// 	  var  ind = this.indexOf(val);
	var ind = this.queryOf(val);
	if(ind > -1) {
		this.splice(ind, 1);
	}
}
//  判断是否存在 某个js 文件
function isInclude(name) {
	var js = /js$/i.test(name);
	var es = document.getElementsByTagName(js ? 'script' : 'link');
	for(var i = 0; i < es.length; i++)
		if(es[i][js ? 'src' : 'href'].indexOf(name) != -1) return true;
	return false;
}

function isLoad(url, add, f) {
	var urlAry = url.split("/");
	var name = urlAry[urlAry.length - 1];
	var typeAry = name.split('.');
	var type = name[typeAry.length - 1];

	if(isInclude(name)) {
		f(url, "检查到有该文件！");
	} else {
		// false
		if(type == 'js') {
			var head = document.getElementsByTagName('html').item(0);
			var Script = document.createElement('script');
			Script.type = "text/javascript";
			Script.src = url;
			head.appendChild(Script);
		} else {
            var head = document.getElementsByTagName('html').item(0);
			var Link = document.createElement('link');
			Link.rel ="stylesheet";
			Link.href = url;
			head.appendChild(Link);
		}

		f(url, "加载文件成功！");
	}

}
/* 按数量拆分数组元素
 * ary - 数据源
 * num  - 每个页数量 
 * return -  Ary
 */
function showSplitData(ary, num) {
	//   debugger;
	var sources = [];
	// let posPattern = /^\d+$/; //正整数正则
	// var source = [];
	var i = 0;
	while(i < ary.length) {
		sources.push(ary.slice(i, i += num));
	};
	return sources;
};

/**
 * 公共请求方法- 使用请记得引用 jQ
 * @method  requestData
 * @param {String}  type   请求类型：get/post
 * @param {Object}  params 请求参数
 * @param {String}  url    请求url 
 * @param {function} f     一个回调信息格式:function(id, data, msg)
 */
function requestURLData(type, url, params, f) {
	$.ajax({
		url:  urlPrefix +  url,
		type: type,
		data: params,
		contentType:"application/json;charset=utf-8",
		dataType: "json",
		success: function(r) {
			console.log(url, "获取到数据->", r);
			if(r.code == 0) {
				f(url,r.data, '请求成功!');
				
			} else {
				console.warn("异常情况:", r.code);
//				f(url,r.code, r.msg);
				alert(r.msg);
			}

		},
		error: function(xmlR, status, error) {
			console.log("\n -->options：", this);
			console.warn(url, "\n -->错误：", error, "\n -->状态", status);
			alert(error);
		},
	});
}